﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// SQlHelper 的摘要说明
/// </summary>
public class SQlHelper
{
    public static string sql = System.Configuration.ConfigurationManager.AppSettings["strconn"].ToString().Trim();
    //复制
    public SQlHelper()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }


    protected SqlConnection getConn() // 链接对象
    {
        SqlConnection conn = new SqlConnection(sql);
        return conn;
    }


    protected int ExeNonquery(string sql)   // 创建command对象 并且执行
    {
        SQlHelper hp = new SQlHelper();
        SqlConnection conn = hp.getConn();
        int result = 0;
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            result = (int)comm.ExecuteNonQuery();

        }
        catch (SqlException ex)    //异常处理
        {
            throw new Exception(ex.Message);   //抛出异常
        }
        finally
        {
            conn.Close();
        }
        return result;




    }
    protected int ExeScalar(string sql) //创建command对象 
    {
        SQlHelper hp = new SQlHelper();
        SqlConnection conn = hp.getConn();
        int result = 0;
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            result = (int)comm.ExecuteScalar();

        }
        catch (SqlException ex)    //异常处理
        {
            throw new Exception(ex.Message);   //抛出异常
        }
        finally
        {
            conn.Close();
        }
        return result;

        //


    }
    protected SqlDataReader Exereader(string sql) // 
    {
        SQlHelper hp = new SQlHelper();
        SqlConnection conn = hp.getConn();
        SqlDataReader read = null;
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            read = comm.ExecuteReader();

        }
        catch (SqlException ex)    //异常处理
        {
            throw new Exception(ex.Message);   //抛出异常
        }
        finally
        {
            conn.Close();
        }
        return read;
    }

    protected DataSet GetDs(string sql)
    {
        SQlHelper hp = new SQlHelper();
        SqlConnection conn = hp.getConn();
        DataSet ds = new DataSet();
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
            adp.Fill(ds);
        }
        catch (SqlException ex)  //异常处理
        {
            throw new Exception(ex.Message);  //抛出异常
        }
        finally
        {
            conn.Close();
        }
        return ds;
    }
}